/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.wii.elpro.web.action;

import com.wii.elpro.dao.QuotationDAOInterface;
import com.wii.elpro.dao.hb.QuotationDAOImpl;
import com.wii.elpro.dao.hb.UserDAOImpl;
import com.wii.elpro.entity.Quotation;
import com.wii.elpro.web.form.QuotationGetQuotationByUserAndStatusForm;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;

/**
 *
 * @author hp02
 */
public class QuotationGetQuotationsByUserAndStatusAction extends Action {
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) throws Exception {
        String forward;
        QuotationDAOInterface dao = new QuotationDAOImpl();
        QuotationGetQuotationByUserAndStatusForm f = (QuotationGetQuotationByUserAndStatusForm) form;
        HttpSession sess = request.getSession(true);
        ActionMessages messages = new ActionMessages();
        try {
            List<Quotation> quotations = dao.getQuotations(new UserDAOImpl().getUser(f.getSales()), f.getStatus());
            request.setAttribute("quotations", quotations);
            String status;
            if(quotations.get(0).getStatus()==0)
                status = "Tender";
            else if(quotations.get(0).getStatus()==1)
                status = "Win";
            else 
                status = "lose";
            messages.add("customMessage", new ActionMessage("customMessage", "Quotation with status "+status+" by "+quotations.get(0).getSpdcf().getInquery().getSales()));
            forward = "success";
        } catch (Exception ex) {
            messages.add("message", new ActionMessage("customMessage", "Application is fail to retrieve data. Please contact my developer."));
            ex.printStackTrace();
            forward = "fail";
        }
        saveMessages(sess, messages);
        return mapping.findForward(forward);
    }
}
